package cn.yzgy.yzgyoa.common.utils;


import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * 代码生成器
 * @author Yqsen
 * @since 2019-8-12
 */
public class CodeGeneratorMid {

    private static final String PACKAGE_NAME = "cn.yzgy.yzgyoa";
    private static final String OUT_PATH = "C:\\Users\\chs_n\\Desktop\\SCGNGMS\\src\\main\\java";
    private static final String MAPPER_PATH = "C:\\Users\\chs_n\\Desktop\\SCGNGMS\\src\\main\\resources";
    private static final String AUTHOR = "Yqsen";

    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    private static final String URL = "jdbc:mysql://120.79.12.140:83/nb_center?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8";
    private static final String USER_NAME = "scgas";
    private static final String PASSWORD = "scgas@2018";

    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.print(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }


    public static void main(String[] args) {
        System.out.println("SCGAS MybatisPlus CRUD代码生成器.");
        // 自定义需要填充的字段
        while(true){
            generate(scanner("请输入模块名："),scanner("请输入表名："));
        }


    }

    private static void generate(String moduleName, String tableName){
        AutoGenerator mpg = new AutoGenerator();
        //数据源配置
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl(URL).setDriverName(DRIVER).setUsername(USER_NAME).setPassword(PASSWORD);
        mpg.setDataSource(dataSourceConfig);

        //全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        // 输出目录
        globalConfig.setOutputDir(OUT_PATH)
                // 是否覆盖文件
                .setFileOverride(true)
                // 开启 activeRecord 模式
                .setActiveRecord(true)
                // XML 二级缓存
                .setEnableCache(false)
                // XML ResultMap
                .setBaseResultMap(false)
                // XML columList
                .setBaseColumnList(true)
                .setAuthor(AUTHOR)
                .setXmlName("%sMapper")
                .setMapperName("%sMapper");
        mpg.setGlobalConfig(globalConfig);

        //包配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setModuleName(moduleName)
                // 自定义包路径
                .setParent(PACKAGE_NAME)
                // 这里是控制器包名，默认 web
                .setController("controller");
        mpg.setPackageInfo(packageConfig);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                // to do nothing
            }
        };
        List<FileOutConfig> focList = new ArrayList<>();
        focList.add(new FileOutConfig("/templates/code_templates/mapper.xml.ftl") {

            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return MAPPER_PATH + "/mapper/" + tableInfo.getEntityName() + "Mapper.xml";

            }
        });

        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);
        mpg.setTemplate(new TemplateConfig().setXml(null));

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        //表名生成策略
        strategy.setNaming(NamingStrategy.underline_to_camel);
//        strategy.setDat
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        strategy.setSuperControllerClass("cn.yzgy.yzgyoa.common.controller.BaseController");
        strategy.setInclude(tableName);
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setEntityLombokModel(true);
        strategy.setEntityBuilderModel(true);
        strategy.setTablePrefix("");
        mpg.setStrategy(strategy);

        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

}
